/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import cn.webrx.config.WebInit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * <p>Project: smvc2022 - WebDemo
 * <p>Powered by webrx On 2022-04-08 14:14:43
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */

@DisplayName("测试Web项目")
@SpringJUnitWebConfig(WebInit.class)
public class WebDemo {
    @Autowired
    private WebApplicationContext wctx;

    private MockMvc mockMvc;

    @BeforeEach
    public void init(){
        mockMvc = MockMvcBuilders.webAppContextSetup(wctx).build();
    }


    @Test
    public void t5() throws Exception {
        //测试 http://localhost/ post请求
        ResultActions action = mockMvc.perform(MockMvcRequestBuilders.post("/home/m2").param("id","88").param("name","李强").header("token","123"));
        MvcResult result = action.andReturn();
        System.out.println(result.getResponse().getContentAsString());
    }

    @Test
    public void t4() throws Exception {
        //测试 http://localhost/ post请求
        ResultActions action = mockMvc.perform(MockMvcRequestBuilders.post("/"));
        MvcResult result = action.andReturn();
        System.out.println(result.getResponse().getContentAsString());
    }

    @Test
    public void t3() throws Exception {
        //测试 http://localhost/home/m1 post请求
        ResultActions action = mockMvc.perform(MockMvcRequestBuilders.post("/home/m1"));
        MvcResult result = action.andReturn();
        System.out.println(result.getResponse().getContentAsString());
    }

    @Test
    public void t2(){
        System.out.println(mockMvc);
    }

    @Test @DisplayName("查看列出所有控制器bean")
    public void t1(){
        for (String n : wctx.getBeanDefinitionNames()) {
            //System.out.println(n);
        }

        System.out.println("-----------------------------------------");
        //System.out.println(wctx.getApplicationName());
    }
}
